<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use app\index\validate\Valid;

class Syspenwarn extends Common
{

	//列表
    public function index()
    {

        $stuff = $this->request->get('stuff');

        $this->assign(array('stuff'=>$stuff));
        $where = [];

        if($stuff){
            //根据模糊查询规则把admin对应的id全部查询出来
            $ids = Db::query("select id from employee where em_name like '%{$stuff}%'");
            $str = [];
            foreach($ids as $k=>$v){
                if($k==0)
                    $str[] = " like '%\"$v[id]\"%' ";
                else
                    $str[] = " stuff like '%\"$v[id]\"%' ";
            }
            $str = implode('or',$str);

            $where['stuff'] = array('exp',$str);
        }

        $count = Db::table('cg_system_penwarn')->where($where)->count();
        $data = Db::table('cg_system_penwarn')->order("id desc")->where($where)->paginate(10,$count);
//        echo Db::table('cg_system')->getLastSql();
        $page = $data->render();
        $data = $data->items();
        $maxpage=ceil($count/10);
        foreach($data as $k=>$v){
            $data[$k]['cusername'] = Db::table('users')->where(array('id'=>$v['cuser']))->value('login_name');
            $data[$k]['pusername'] = Db::table('users')->where(array('id'=>$v['puser']))->value('login_name');
            $temp = unserialize($v['stuff']);
            $temp = implode(',',$temp);
            $temp = Db::query("select em_name from employee where id in(".$temp.")");
            $temp2 = [];
            foreach($temp as $v1){
                $temp2[] = $v1['em_name'];
            }
            $data[$k]['stuff'] = implode(',',$temp2);
            $temp = unserialize($v['type']);
            foreach($temp as $k1=>$v1){
                $temp[$k1] = $v1==0?'邮件':'弹窗';
            }
            $data[$k]['type'] = implode(',',$temp);
            $data[$k]['ctime'] = date('Y-m-d H:i',$v['ctime']);
            $data[$k]['ptime'] = date('Y-m-d H:i',$v['ptime']);
        }

        $this->assign('data',$data);
        $this->assign('page',$page);
        $this->assign('maxpage',$maxpage);
        $this->assign('count',$count);
        return $this->fetch('');
    }
    public function add(){
        if($_POST){
            $validate = new Valid([
                'stuff'  => 'require',
                'type'  => 'require',
            ]);
            $data = [];
            $data['stuff'] = $this->request->post('stuff');
            $data['type'] = $this->request->post('type/a');

            if (!$validate->check($data)) {
                echo  json_encode([
                    "success"=>false,
                    "msg"=>$validate->getError()
                ]);die;
            }

            $data['stuff'] = explode(',',$data['stuff']);
            $data['stuff'] = serialize($data['stuff']);
            $data['type'] = serialize($data['type']);
            $data['cuser'] = $_SESSION['think']['employee_id'];
            $data['ctime'] = time();
            $data['puser'] = $_SESSION['think']['employee_id'];
            $data['ptime'] = time();

            $bool = Db::table('cg_system_penwarn')->insert($data);
            if(false !== $bool){
                echo  json_encode([
                    "success"=>true,
                    "msg"=>"添加成功"
                ]);die;
            }else{
                echo  json_encode([
                    "success"=>false,
                    "msg"=>"添加失败"
                ]);die;
            }

        }
        return $this->fetch('form');
    }



    public function update(){
        $id = $this->request->param('id');

        if($_POST){
            $validate = new Valid([
                'stuff'  => 'require',
                'type'  => 'require',
            ]);
            $data = [];
            $data['stuff'] = $this->request->post('stuff');
            $data['type'] = $this->request->post('type/a');

            if (!$validate->check($data)) {
                echo  json_encode([
                    "success"=>false,
                    "msg"=>$validate->getError()
                ]);die;
            }

            $data['stuff'] = explode(',',$data['stuff']);
            $data['stuff'] = serialize($data['stuff']);
            $data['type'] = serialize($data['type']);
            $data['cuser'] = $_SESSION['think']['employee_id'];
            $data['ctime'] = time();
            $data['puser'] = $_SESSION['think']['employee_id'];
            $data['ptime'] = time();
            $bool = Db::table('cg_system_penwarn')->where('id',$id)->update($data);
            if(false !== $bool){
                echo  json_encode([
                    "success"=>true,
                    "msg"=>"修改成功"
                ]);die;
            }else{
                echo  json_encode([
                    "success"=>false,
                    "msg"=>"修改失败"
                ]);die;
            }
            die;
        }

        $data = Db::table('cg_system_penwarn')->where('id',$id)->find();

        if($data['stuff']){
            $data['stuff'] = unserialize($data['stuff']);
            foreach($data['stuff'] as $k=>$v){
                $data['stuff'][$k] = ['em_name'=>Db::table('employee')->where('id',$v)->value('em_name'),'id'=>$v];
            }
        }
        if($data['type']){
            $data['type'] = unserialize($data['type']);
        }

        $this->assign('data',$data);

        return $this->fetch('form');
    }



    public function delete(){
        $id = $this->request->param('id');
        if(!$id){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }
        $bool = Db::table('cg_system_penwarn')->where(array('id'=>$id))->delete();
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"删除成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"删除失败"
            ]);die;
        }
    }



}
